package com.tepth.busfix.main.setting;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.tencent.bugly.beta.Beta;
import com.tepth.busfix.MyApplication;
import com.tepth.busfix.R;
import com.tepth.busfix.base.BaseActivity;
import com.tepth.busfix.bean.reponse.User;
import com.tepth.busfix.config.SpKey;
import com.tepth.busfix.download.AppUpdateManager;
import com.tepth.busfix.main.login.LoginActivity;
import com.tepth.busfix.network.Network;
import com.tepth.busfix.tools.ActivityTaskManager;
import com.tepth.busfix.tools.AppUtils;
import com.tepth.busfix.tools.SPUtils;
import com.tepth.busfix.tools.ToastUtils;
import com.tepth.busfix.tools.UsbUtil;
import com.tepth.busfix.view.dialog.DialogDelegate;
import com.tepth.busfix.view.dialog.SweetAlertDialogDelegate;

import butterknife.BindView;
import butterknife.OnClick;
import cn.carbs.android.library.MDDialog;

/**
 * Created by zhangyu on 2017/3/28.
 */

public class SettingActivity extends BaseActivity implements AppUpdateManager.OnVersionCheckListener {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_group)
    TextView tvGroup;
    @BindView(R.id.tv_user_code)
    TextView tvCode;
    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.tv_company)
    TextView tvCompany;
    @BindView(R.id.tv_factory)
    TextView tvFactory;
    @BindView(R.id.tv_belong_group)
    TextView tvBelongGroup;
    @BindView(R.id.tv_device)
    TextView tvDevice;
    @BindView(R.id.tv_update)
    TextView tvUpdate;
    @BindView(R.id.tv_debug)
    TextView mTvDebug;
    DialogDelegate mDialogDelegate;

    @Override
    public int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    public void initView() {
        ActivityTaskManager.getActivityTaskManager().addActivity(this);
        initToolBar();
        tvTitle.setText(R.string.preferences_name);
        User user = (User) SPUtils.getObject(this, SpKey.KEY_USER_INFO);
        try {
            if (user != null) {
                if (user.getGroupInfo() != null) {
                    tvGroup.setText(user.getGroupInfo().getName());
                }
                if (user.getCode() != null) {
                    tvCode.setText(user.getCode());
                }
                if (user.getLoginName() != null) {
                    tvAccount.setText(SPUtils.getString(this, SpKey.KEY_USER_ACCOUNT, ""));
                }
                if (user.getOrgInfo() != null) {
                    tvCompany.setText(user.getOrgInfo().getName());
                }
                if (user.getDeptInfo() != null) {
                    tvFactory.setText(user.getDeptInfo().getName());
                }
                if (user.getGroupInfo() != null) {
                    tvBelongGroup.setText(user.getGroupInfo().getName());
                }
                tvUpdate.setText(AppUtils.getVersion(this) + "");
                if (SPUtils.getBoolean(MyApplication.getContext(), "isDebug")) {
                    mTvDebug.setText("已连接测试服务器!点击切换为正式服务器");
                } else {
                    mTvDebug.setText("已连接正式服务器!点击切换为测试服务器");
                }
                mDialogDelegate = new SweetAlertDialogDelegate(this);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void initToolBar() {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void setPresenter() {

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }

    @OnClick(R.id.btn_logout)
    public void onLogout() {

        Intent intent = new Intent(this, LoginActivity.class);
        intent.putExtra("reload", "reload");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    @OnClick(R.id.layout_update)
    public void onUpdate() {
        Beta.checkUpgrade(true, false);
    }

    @OnClick(R.id.layout_device)
    public void onDevice() {
        if (!UsbUtil.hasUsbDevice(this)) {
            startActivity(new Intent(this, NFCListActivity.class));
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        tvDevice.setText(SPUtils.getString(this, SpKey.KEY_NFC_BIND_MAC, ""));
    }

    @OnClick(R.id.btn_exit)
    public void onExit() {
        String[] messageContent = new String[]{"是否退出"};
        new MDDialog.Builder(this)
                .setMessages(messageContent)
                .setTitle("提示")
                .setNegativeButton(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                })
                .setPositiveButton("退出", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                            ActivityTaskManager.getActivityTaskManager().finishActivity();
//                            finish();
                        ActivityTaskManager.getActivityTaskManager().appExit(SettingActivity.this);
                    }
                })
                .setWidthMaxDp(600)
                .setShowButtons(true)
                .create()
                .show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityTaskManager.getActivityTaskManager().finishActivity(this);
    }

    @Override
    public void onSucceed(String msg) {
        ToastUtils.showToast(this, msg);
    }

    @Override
    public void onFailed(String msg) {
        onSucceed(msg);
    }

    @OnClick(R.id.tv_debug)
    public void onChangeDebug() {
        final boolean isDebug = SPUtils.getBoolean(MyApplication.getContext(), "isDebug", false);
        mDialogDelegate.showWarningDialog(isDebug ? "切换为正式服务器" : "切换为测试服务器", "确认切换将会重新登录？", new DialogDelegate.OnDialogListener() {
            @Override
            public void onClick() {
                Network.changeForDebug(!isDebug);
                SPUtils.putBoolean(MyApplication.getContext(), "isDebug", !isDebug);
                mTvDebug.setText(!isDebug ? "已连接测试服务器!点击切换为正式服务器" : "已连接正式服务器!点击切换为测试服务器");
                mDialogDelegate.clearDialog();
                onLogout();
            }
        });
    }
}
